//
//  MyBidJobDetailTabDetailSectionJobDetail.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/4/18.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift
class ServiceDetailTabDetailSectionJobDetail: CustomBackView {
    private lazy var disposeBag = DisposeBag()
    var previewImagesClosure: ((Int)->Void)?
    //job detail
    let subjectLabel = ContentLabel(R.font.interBold(size: 18), hexStr: "#FFFFFF")
    let jobPriceLabel = ContentLabel(R.font.interMedium(size: 20), hexStr: "#FFFFFF")
    let jobStatusLabel = ContentLabel(R.font.interMedium(size: 13), hexStr: "#FFFFFF")
    let paymentStatusLabel = PaddingLabel()
    let customerNameLabel = ContentLabel(R.font.interBold(size: 14))
    let addressLabel = ContentLabel(R.font.interMedium(size: 13), hexStr: "#292D32B2")
    let phoneLabel = ContentLabel(R.font.interMedium(size: 13), hexStr: "#292D32B2")
    let dueDateLabel = ContentLabel(R.font.interMedium(size: 13), hexStr: "#292D32B2")
    //
    let detailLabel = ContentLabel(R.font.interMedium(size: 13), hexStr: "#292D32B2")
    //
    let noteLabel = ContentLabel(R.font.interMedium(size: 13), hexStr: "#292D32B2")
    
    let callPhoneBtn = UIButton()
    let sendMsgBtn = UIButton()
    //images
    let imagesCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0),collectionViewLayout: UICollectionViewFlowLayout())
    var picturesCollectionViewHeightConstant: Constraint?
    override func setUpView() {
        self.drawCorner(byRoundingCorners: .allCorners)
        //1.1 第一段top backview
        let topBackView = UIView()
        topBackView.drawCorner(byRoundingCorners: [.topLeft, .topRight], radii: 10, backColorHexStr: "#688F4E")
        self.addSubview(topBackView)
        topBackView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        //1.1.2
        subjectLabel.textColor = UIColor.white
        subjectLabel.setFont(R.font.interBold(size: 18))
        subjectLabel.numberOfLines = 0
        subjectLabel.textAlignment = .left
        topBackView.addSubview(subjectLabel)
        subjectLabel.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //1.1.3
        jobPriceLabel.textAlignment = NSTextAlignment.left
        jobPriceLabel.textColor = .white
        jobPriceLabel.setFont(R.font.lexendMedium(size: 20))
        topBackView.addSubview(jobPriceLabel)
        jobPriceLabel.snp.makeConstraints { (make) in
            make.top.equalTo(subjectLabel.snp.bottom).offset(4)
            make.left.equalTo(subjectLabel)
            make.width.greaterThanOrEqualTo(0)
            make.height.equalTo(30)
            make.bottom.equalTo(-10)
        }
        //1.1.3.1 payment status
        paymentStatusLabel.setFont(R.font.interMedium(size: 12))
        paymentStatusLabel.textColor = .white
        paymentStatusLabel.layer.borderColor = UIColor.white.cgColor
        paymentStatusLabel.layer.borderWidth = 1
        paymentStatusLabel.layer.cornerRadius = 3
        //ContentLabel(, hexStr: "#FFFFFF")
        topBackView.addSubview(paymentStatusLabel)
        paymentStatusLabel.snp.makeConstraints { (make) in
            make.left.equalTo(jobPriceLabel.snp.right).offset(20)
            make.width.height.greaterThanOrEqualTo(0)
            make.centerY.equalTo(jobPriceLabel)
        }
        //1.1.1 job status
        topBackView.addSubview(jobStatusLabel)
        jobStatusLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(paymentStatusLabel)
            make.right.equalTo(-16)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //第二段：job detail的其他信息
        self.addSubview(customerNameLabel)
        customerNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(topBackView.snp.bottom).offset(16)
            make.left.equalTo(16)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //1.1.4.1
        let addressImageView = UIImageView()
        self.addSubview(addressImageView)
        addressImageView.image = UIImage(named: "service_bidjob_detail_address_icon")
        addressImageView.snp.makeConstraints { (make) in
            make.left.equalTo(customerNameLabel)
            make.top.equalTo(customerNameLabel.snp.bottom).offset(8)
            make.height.width.equalTo(16)
        }
        //1.1.4.2
        addressLabel.numberOfLines = 0
        addressLabel.textAlignment = .left
        self.addSubview(addressLabel)
        addressLabel.snp.makeConstraints { (make) in
            make.left.equalTo(addressImageView.snp.right).offset(8)
            make.height.greaterThanOrEqualTo(addressImageView)
            make.right.equalTo(-16)
            make.top.equalTo(addressImageView)
        }
        //1.1.5.1
        let timeImageView = UIImageView()
        self.addSubview(timeImageView)
        timeImageView.image = UIImage(named: "service_bidjob_detail_time_icon")
        timeImageView.snp.makeConstraints { (make) in
            make.top.equalTo(addressLabel.snp.bottom).offset(8)
            make.left.size.equalTo(addressImageView)
        }
        //1.1.5.2
        self.addSubview(dueDateLabel)
        dueDateLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(timeImageView)
            make.left.equalTo(addressLabel)
            make.height.greaterThanOrEqualTo(timeImageView)
        }
        //1.1.6.1
        let phoneImageView = UIImageView(image: UIImage(named: "service_myjob_detail_phone_icon"))
        self.addSubview(phoneImageView)
        phoneImageView.snp.makeConstraints { (make) in
            make.top.equalTo(dueDateLabel.snp.bottom).offset(8)
            make.left.size.equalTo(addressImageView)
        }
        //1.1.6.2 phone label
        self.addSubview(phoneLabel)
        phoneLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(phoneImageView)
            make.left.equalTo(addressLabel)
            make.height.greaterThanOrEqualTo(phoneImageView)
        }
        //1.1.6.4 send msg button
        sendMsgBtn.setImage(UIImage(named: "service_myjob_detail_sendmsg_icon"), for: UIControl.State.normal)
        self.addSubview(sendMsgBtn)
        sendMsgBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-16)
            make.height.width.equalTo(40)
            make.bottom.equalTo(phoneImageView)
        }
        //1.1.6.3 call phone button
        callPhoneBtn.setImage(UIImage(named: "service_myjob_detail_callphone_icon"), for: UIControl.State.normal)
        self.addSubview(callPhoneBtn)
        callPhoneBtn.snp.makeConstraints { (make) in
            make.right.equalTo(sendMsgBtn.snp.left).offset(-16)
            make.height.width.equalTo(sendMsgBtn)
            make.centerY.equalTo(sendMsgBtn)
        }
        //detail label
        let detailTitle = ContentLabel(R.font.interBold(size: 14))
        detailTitle.text = "Detail"
        self.addSubview(detailTitle)
        detailTitle.snp.makeConstraints { (make) in
            make.top.equalTo(phoneImageView.snp.bottom).offset(20)
            make.left.equalTo(customerNameLabel)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //
        detailLabel.textAlignment = .left
        detailLabel.numberOfLines = 0
        self.addSubview(detailLabel)
        detailLabel.snp.makeConstraints { (make) in
            make.top.equalTo(detailTitle.snp.bottom).offset(8)
            make.left.equalTo(detailTitle)
            make.right.equalTo(-16)
            make.height.greaterThanOrEqualTo(20)
        }
        //note
        let noteTitle = ContentLabel(R.font.interBold(size: 14))
        noteTitle.text = "Note"
        self.addSubview(noteTitle)
        noteTitle.snp.makeConstraints { (make) in
            make.top.equalTo(detailLabel.snp.bottom).offset(20)
            make.left.equalTo(detailTitle)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //
        noteLabel.numberOfLines = 0
        noteLabel.textAlignment = .left
        self.addSubview(noteLabel)
        noteLabel.snp.makeConstraints { (make) in
            make.top.equalTo(noteTitle.snp.bottom).offset(8)
            make.left.right.equalTo(detailLabel)
            make.height.greaterThanOrEqualTo(20)
        }
        //picture
        let pictureTitle = ContentLabel(R.font.interBold(size: 14))
        pictureTitle.text = "Pictures"
        self.addSubview(pictureTitle)
        pictureTitle.snp.makeConstraints { (make) in
            make.top.equalTo(noteLabel.snp.bottom).offset(20)
            make.left.equalTo(detailTitle)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //1.3.4
        self.addSubview(imagesCollectionView)
        imagesCollectionView.snp.makeConstraints { (make) in
            make.top.equalTo(pictureTitle.snp.bottom).offset(8)
            make.left.right.equalTo(detailLabel)
            self.picturesCollectionViewHeightConstant = make.height.greaterThanOrEqualTo(10).constraint
            make.bottom.equalToSuperview().offset(-16)
        }
        //设置collection view信息
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 7
        layout.minimumLineSpacing = 7
        layout.scrollDirection = UICollectionView.ScrollDirection.vertical
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.itemSize = CGSize(width: ServiceDetailImageCollectionViewCell.cellSize, height: ServiceDetailImageCollectionViewCell.cellSize)
        imagesCollectionView.setCollectionViewLayout(layout, animated: true)
        imagesCollectionView.translatesAutoresizingMaskIntoConstraints = false
        imagesCollectionView.backgroundColor = UIColor.clear
        imagesCollectionView.isScrollEnabled = false
        imagesCollectionView.register(ServiceDetailImageCollectionViewCell.self, forCellWithReuseIdentifier: ServiceDetailImageCollectionViewCell.reuseCellIdentifier)
        //绑定imagesCollectionView的
        imagesCollectionView.rx.observe(CGSize.self,"contentSize").subscribe {[weak self] (size) in
            guard let contentSize = size else {return}
            self?.picturesCollectionViewHeightConstant?.update(offset: contentSize.height)
        }.disposed(by: self.disposeBag)
        //绑定image数据源
        ServiceDetailModelHelper.shared.images.bind(to: imagesCollectionView.rx.items){(collectionView, row, element) in
            let indexPath = IndexPath(row: row, section: 0)
            if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ServiceDetailImageCollectionViewCell.reuseCellIdentifier,
                                                             for: indexPath) as? ServiceDetailImageCollectionViewCell{
                cell.setData(element.image)
                return cell
            }else{
                return UICollectionViewCell()
            }
            
        }.disposed(by: disposeBag)
        //选择某一行
        imagesCollectionView.rx.itemSelected.bind{ [weak self](indexPath) in
            self?.previewImagesClosure?(indexPath.row)
        }
        .disposed(by: disposeBag)
    }
    func setData(_ model: ServiceDetailModel){
        //owner detail
        subjectLabel.text = model.title
        customerNameLabel.text = (model.owner_address.first_name == "" ? "": (model.owner_address.first_name + " ")) + model.owner_address.last_name
        addressLabel.text = model.owner_address.address
        phoneLabel.text = model.owner_address.phone
        paymentStatusLabel.text = model.payment_status
        jobStatusLabel.text = model.status.localizedCapitalized
        //去掉小数点，只显示整数部分
        if model.budget == "0" || model.budget == ""{//不显示价格
            jobPriceLabel.text = ""
            paymentStatusLabel.snp.remakeConstraints { (make) in
                make.left.equalTo(jobPriceLabel)
                make.width.height.greaterThanOrEqualTo(0)
                make.centerY.equalTo(jobPriceLabel)
            }
            //1.1
        }else{
            let realPrice = Tool().formatFloatStrToCurrency(model.budget)
            jobPriceLabel.text = "$\(realPrice)"
            paymentStatusLabel.snp.remakeConstraints { (make) in
                make.left.equalTo(jobPriceLabel.snp.right).offset(20)
                make.width.height.greaterThanOrEqualTo(0)
                make.centerY.equalTo(jobPriceLabel)
            }
        }
        
        //
        var installDate = model.due_date
        if model.scheduler_time != "" {
            installDate = DateUtil().convertDateStrToDateStr(dateStr: model.scheduler_time, oldFormat: "MMM dd yyyy HH:mm:ss", newFormat: "MMM dd yyyy")
        }
        dueDateLabel.text = installDate
        detailLabel.text = model.detail
        noteLabel.text = model.note
    }
}
